package com.c.ctgproject.ui.collect

import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityBatteryHandlerSelectDataBinding
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.bigkoo.pickerview.builder.TimePickerBuilder
import com.c.ctgproject.entity.guang.BatteryCanHandlerEntity
import com.c.ctgproject.event.PositionsEvent
import com.c.ctgproject.utils.*
import org.greenrobot.eventbus.EventBus
import java.text.SimpleDateFormat
import java.util.*
import java.util.logging.SimpleFormatter
import kotlin.collections.ArrayList
import kotlin.collections.HashMap


/**
 *作者:LiBW
 *创建日期: 2021/9/13
 *描述: 电池拆解 页面(选择日期)
 */
class BatteryHandlerSelectDataActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityBatteryHandlerSelectDataBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val pbIds =ArrayList<Int>()

    private val  selectList by lazy { intent.getSerializableExtra(MyConstants.DATA) as ArrayList<BatteryCanHandlerEntity.DataBean.ListBean> }

    private var time = ""
    override fun event() {
        selectList.forEach {
            pbIds.add(it.pbId)
        }

        val startDate = Calendar.getInstance()
        val endDate = Calendar.getInstance()
//        startDate[2013, 0] = 1
        endDate[2030, 11] = 31
        val pvTime = TimePickerBuilder(this) { date, v ->
            val format = SimpleDateFormat("yyyy-MM-dd", Locale.CHINA)
            time = format.format(date)
            mBinding.mTimeTV.text = time
            MyLog.d("选择的时间:$date  ${getGson().toJson(date)}")
        }.setRangDate(startDate, endDate)
            .build()
        mBinding.mTimeLL.setOnClickListener {
            pvTime.show()
        }

    }

    override fun click() {
        mBinding.mConfirmBTN.setOnClickListener {
            if (time.isEmpty()) {
                showToast("请选择计划完成时间")
                mBinding.mTimeLL.myShake()
            } else {
                val map = HashMap<String, Any>()
                map["pbIdList"]=pbIds
                map["plainFinishTime"]=time
                ApiMethods.apiSubscribe(ApiMethods.retrofit.createBatteryHandlerOrderV2(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                    MyLog.d("拆解回调:$it")
                    showToast("操作成功")
                    val event=PositionsEvent(pbIds)
                    EventBus.getDefault().post(event)
                    finish()
                })
            }
        }
    }
}